﻿using System;
using Reader.Core.DataAccessObject.Models;
using Microsoft.EntityFrameworkCore;


namespace Reader.Core.DataAccessObject
{
    public partial class ApplicationDBContext : DbContext
    {
        public ApplicationDBContext() : base() { }
        public ApplicationDBContext(DbContextOptions<ApplicationDBContext> options) : base(options)
        {

        }

        protected override void OnConfiguring(DbContextOptionsBuilder options)
        {
#if DEBUG
            options.UseSqlite(@$"Data Source=E:\GitHub\阅读器\Reader\Reader.loc", option =>
            {
                option.CommandTimeout(30);
            });
#else
            options.UseSqlite("Data Source=Reader.loc", option =>
            {
                option.CommandTimeout(30);
            });
#endif
        }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
#region 字段处理
            modelBuilder.Entity<Book>().Property(m => m.UpdateInfo).HasDefaultValue(0);
            modelBuilder.Entity<Book>().Property(m => m.Completed).HasDefaultValue(0);

            modelBuilder.Entity<BookShelf>().HasIndex(m => m.Name).IsUnique(true);

            modelBuilder.Entity<Author>().HasIndex(m => m.Name).IsUnique(true);

            modelBuilder.Entity<BookSite>().Property(m => m.Rule).HasDefaultValue(0);
            modelBuilder.Entity<BookSite>().Property(m => m.Speed).HasDefaultValue(0);
#endregion
            base.OnModelCreating(modelBuilder);
        }
    }
}
